import { getAllProperty } from "@/apis/property";
import { getAllDictionary } from "@/apis/dictionary";

const state = {
  propertyInfo: JSON.parse(sessionStorage.getItem("propertyInfo")),
  propertyList: [],
  propertyTypeList: []
};

const mutations = {
  SET_PROPERTYINFO: (state, propertyInfo) => {
    state.propertyInfo = propertyInfo;
  },
  SET_PROPERTYLIST: (state, propertyList) => {
    state.propertyList = propertyList;
  },
  SET_BUILDTYPELIST: (state, propertyTypeList) => {
    state.propertyTypeList = propertyTypeList;
  }
};

const actions = {
  setProperty({ commit }, propertyInfo) {
    commit("SET_PROPERTYINFO", propertyInfo);
  },
  getPropertyList({ commit }) {
    return new Promise(async resolve => {
      const { rows } = await getAllProperty();
      commit("SET_PROPERTYLIST", rows);
      resolve(rows);
    });
  },
  async getBuildTypeList({ commit }) {
    const { rows } = await getAllDictionary({ fieldName: "propertyType" });
    commit("SET_BUILDTYPELIST", rows);
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
